package com.abel.web.validator;

import java.text.SimpleDateFormat;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.abel.web.view.PersonalInfoViewBean;

public class PersonalInfoViewValidator implements Validator{

	@SuppressWarnings("rawtypes")
	@Override
	public boolean supports(Class clazz) {
		return PersonalInfoViewBean.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {

		PersonalInfoViewBean viewBean = (PersonalInfoViewBean)obj;

		if (viewBean.getSsn() == null
				|| viewBean.getSsn().equals("")) {
			errors.rejectValue("ssn", "error.null",null,"SSN is required field");
		}

		if (viewBean.getSsn() != null
				&& !viewBean.getSsn().matches("[0-9]{9}")) {
			errors.rejectValue("ssn", "error.null",null,"Please enter a valid SSN in the format 123456789");
		}

		if (viewBean.getFirstName() == null
				|| viewBean.getFirstName().equals("")) {
			errors.rejectValue("firstName", "error.null",null,"First Name is required field");
		}
		
		if (viewBean.getFirstName() != null
				&& !viewBean.getFirstName().equals("")
				&& !viewBean.getFirstName().matches("^[a-zA-Z\\s]+$")) {
			errors.rejectValue("firstName", "error.null",null,"First Name cannot have invalid characters");
		}
		
		if (viewBean.getMi() != null
				&& !viewBean.getMi().matches("[a-zA-Z]{1}")) {
			errors.rejectValue("mi", "error.null",null,"Enter a valid middle initial");
		}

		if (viewBean.getLastName() == null
				|| viewBean.getLastName().equals("")) {
			errors.rejectValue("lastName", "error.null",null,"Last Name is required field");
		}
		
		if (viewBean.getLastName() != null
				&& !viewBean.getLastName().equals("")
				&& !viewBean.getLastName().matches("[a-zA-z]+([ '-][a-zA-Z]+)*")) {
			errors.rejectValue("lastName", "error.null",null,"Last Name cannot have invalid characters");
		}
		
		if (viewBean.getDateOfBirth() != null) {
			
			try {
				new SimpleDateFormat("MM/dd/yyyy").parse(viewBean.getDateOfBirth());
			}
			catch (Exception e) {
				errors.rejectValue("dateOfBirth", "error.null",null,"Invalid date of birth format.  Enter in the format mm/dd/yyyy");
			}
			
		}
		//TODO remove compulsory fields for alt phone, work phone and middile initial from jsp

		if (viewBean.getAddress1() == null
				|| viewBean.getAddress1().equals("")) {
			errors.rejectValue("address1", "error.null",null,"One of the address lines have to be used for address");
		}
		
		if (viewBean.getCity() == null
				|| viewBean.getCity().equals("")) {
			errors.rejectValue("city", "error.null",null,"City is required field");
		}
		
		if (viewBean.getCity() != null
				&& !viewBean.getCity().equals("")
				&&!viewBean.getCity().matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)")) {
			errors.rejectValue("city", "error.null",null,"Valid city name required");
		}

		if (viewBean.getZip() == null
				|| viewBean.getZip().equals("")) {
			errors.rejectValue("zip", "error.null",null,"Zip Code is required field");
		}
		
//		if (viewBean.getZip() != null
//				&& !viewBean.getZip().equals("")
//				&& !viewBean.getZip().matches("\\d{5}(-\\d{4})?")) {
//			
//		}

		if (viewBean.getHomePhone() == null
				|| viewBean.getHomePhone().equals("")) {
			errors.rejectValue("homePhone", "error.null",null,"Home phone is required field");
		}
		
//		if (viewBean.getHomePhone() != null 
//				&& !viewBean.getHomePhone().equals("") 
//				&& !viewBean.getHomePhone().matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}")) {
//			errors.rejectValue("homePhone", "error.null",null,"Valid Home phone required");
//		}

//		if (viewBean.getAltPhone() != null 
//				&& !viewBean.getAltPhone().equals("") 
//				&& !viewBean.getAltPhone().matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}")) {
//			errors.rejectValue("altPhone", "error.null",null,"Valid Alternate phone required");
//		}
//
//		if (viewBean.getWorkPhone() != null 
//				&& !viewBean.getWorkPhone().equals("") 
//				&& !viewBean.getWorkPhone().matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}")) {
//			errors.rejectValue("workPhone", "error.null",null,"Valid Work phone required");
//		}

	}
}
